﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;
namespace eStock
{
    public partial class frmImExport : hForm
    {
        public string ID = "";
        public frmImExport()
        {
            InitializeComponent();
            this.SaveButton = btnSave;
        }

        private void frmImExport_Load(object sender, EventArgs e)
        {
            Common.LoadCombo(cboKind, "IEP", false);
            LoadMaster(ID);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Error == true) 
            {
                btnSave.Error = false;
                return;
            }
            SaveMaster();
        }

        private void SaveMaster()
        {
            try
            {
                string ImExportNo = txtImExportNo.Text;
                if (txtImExportNo.Text.Trim().Length == 0)
                {
                    //if (tblImExport.Exec("A", ID, ref ImExportNo, cboKind.SelectedValue.ToString(), txtItemCd.Text, nmrQuantity.Value.ToString()) == 0) 
                    //{
                    //    Dialog.ShowMess(Dialog.mType.err, "Lưu dữ liệu không thành công!");
                    //    return;
                    //}
                }
                else
                {
                    //if (tblImExport.Exec("U", ID, ref ImExportNo, cboKind.SelectedValue.ToString(), txtItemCd.Text, nmrQuantity.Value.ToString()) == 0) 
                    //{
                    //    Dialog.ShowMess(Dialog.mType.err, "Lưu dữ liệu không thành công!");
                    //    return;
                    //}

                }
                Dialog.ShowMess(Dialog.mType.ok, "Đã lưu thành công!");
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("SELECT ID FROM tblImExport with(nolock) where ImExportNo = '" + ImExportNo + "'");
                ID = dt.Rows[0]["ID"].ToString();
                LoadMaster(ID);
            }
            catch (Exception ex)
            {
                Common.GhiLog("SaveMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void LoadMaster(string ID)
        {
            try
            {
                if (ID.Trim().Length == 0)
                {
                    return;
                }
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("SELECT a.*,b.ItemName FROM tblImExport as a WITH(NOLOCK) join tblItem as b with(nolock) on a.ItemCd = b.ItemCd WHERE a.ID = '" + ID + "'");
                txtImExportNo.Text = dt.Rows[0]["ImExportNo"].ToString();
                txtCreateBy.Text = dt.Rows[0]["CreateBy"].ToString();
                txtCreateDate.Text = dt.Rows[0]["CreateDate"].ToString();
                cboKind.SelectedValue = dt.Rows[0]["ImExportKind"].ToString();
                txtItemCd.Text = dt.Rows[0]["ItemCd"].ToString();
                txtItemName.Text = dt.Rows[0]["ItemName"].ToString();
                nmrQuantity.Value = Decimal.Parse(dt.Rows[0]["Quantity"].ToString());
            }
            catch (Exception ex)
            {
                Common.GhiLog("SaveMaster", ex);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
